package com.demo.service;

import com.demo.vo.ApiParam;
import com.demo.vo.BaseResult;
import com.demo.vo.ParamDetail;
import com.demo.vo.TemplatesParam;
import com.yiyi.JsonUtil;
import com.yiyi.StringUtil;
import com.yiyi.http.HttpClientUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sdyang
 * @create 2019-02-26 15:05
 **/
@Service
public class DatasourceService {

    protected static String URL = "http://120.79.200.233/openApi/invoke";

    public  static String query(TemplatesParam param){

        param.setCode("123");
        param.setName("测试");
        param.setTitle("标题");

        ApiParam apiParam = new ApiParam();
        apiParam.setServiceName("com.wolf.querylist.service.impl.DefaultQueryListManager");
        apiParam.setMethodName("queryResult");

        List<ParamDetail> params = new ArrayList<>();

        ParamDetail detail1 = new ParamDetail();
        detail1.setParamName("key");
        detail1.setDataType("java.lang.String");
        detail1.setDataValue("datasource_query_list");
        params.add(detail1);

        Map<String,String> dataMap = new HashMap<>();
        if(StringUtil.isNotEmpty(param.getName())) {
            dataMap.put("data_source_namexxx148xxx", param.getName());
        }

        ParamDetail detail2 = new ParamDetail();
        detail2.setParamName("paramMap");
        detail2.setDataType("java.util.Map");

        detail2.setDataValue(JsonUtil.toJSONString(dataMap));
        params.add(detail2);

        apiParam.setParams(params);
        HashMap<String,String> data = new HashMap<>();
        System.out.println(JsonUtil.toJSONString(apiParam));
        data.put("data", JsonUtil.toJSONString(apiParam));
        String response = HttpClientUtil.post(URL,data);

        System.out.println(response);

        BaseResult result = JsonUtil.parseObject(response,BaseResult.class);
        return  result.getMessage();
    }

    public static void main(String[] args){
        TemplatesParam param = new TemplatesParam();
        param.setName("test");
        query(param);
    }

}
